<template>
  <transition name="fade">
    <div v-if="visible" class="loading-wrapper" @click.self="handleClick">
      <div class="loading-container">
        <div class="loading-spinner">
          <div class="spinner-ring"></div>
          <div class="spinner-ring"></div>
          <div class="spinner-ring"></div>
          <div class="spinner-ring"></div>
        </div>
        <p class="loading-text">正在玩命加载中...</p>
      </div>
    </div>
  </transition>
</template>

<script setup>
import { ref, watch } from 'vue'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  }
})

const emits = defineEmits(['update:visible'])

// 控制显示状态
const handleClick = () => {
  if (props.visible) {
    emits('update:visible', false)
  }
}

// 监听 visible 变化，防止点击背景关闭
watch(() => props.visible, (newVal) => {
  if (newVal) {
    // 显示时禁用背景滚动
    document.body.style.overflow = 'hidden'
  } else {
    // 隐藏时恢复滚动
    document.body.style.overflow = ''
  }
})
</script>

<style scoped>
.loading-wrapper {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}

.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.5rem;
  padding: 2rem 3rem;
  background: white;
  border-radius: 16px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}

.loading-spinner {
  position: relative;
  width: 64px;
  height: 64px;
}

.spinner-ring {
  position: absolute;
  width: 100%;
  height: 100%;
  border: 4px solid transparent;
  border-top: 4px solid;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

.spinner-ring:nth-child(1) {
  border-top-color: #667eea;
  animation-duration: 1s;
}

.spinner-ring:nth-child(2) {
  border-top-color: #764ba2;
  animation-duration: 1.2s;
  animation-direction: reverse;
  width: 80%;
  height: 80%;
  top: 10%;
  left: 10%;
}

.spinner-ring:nth-child(3) {
  border-top-color: #f093fb;
  animation-duration: 1.4s;
  width: 60%;
  height: 60%;
  top: 20%;
  left: 20%;
}

.spinner-ring:nth-child(4) {
  border-top-color: #4facfe;
  animation-duration: 1.6s;
  animation-direction: reverse;
  width: 40%;
  height: 40%;
  top: 30%;
  left: 30%;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.loading-text {
  margin: 0;
  font-size: 1rem;
  font-weight: 500;
  color: #4a5568;
  letter-spacing: 1px;
}

/* 淡入淡出动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

.loading-container {
  animation: slideUp 0.3s ease;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>

